cd ""
set more off
capture: est drop *


///////////////////////////////////////////
// Table 1 
///////////////////////////////////////////

// Note: Tables outputted as .tex after all models have been run

///////////////////////////////////////////
// Belgium
///////////////////////////////////////////

use "./Datasets/Crossnational/Belgium.dta", clear
xtset id year

centile leftshare_begin if year==2004, c(50)
local sd_med = `r(c_1)'

// Table 1
xtreg log_cpas pctforeign logpop log_unemp_rate filing_rate sd_share i.year if leftshare_begin!=., fe cl(id)
est store T1a_1
xtreg log_cpas pctforeign logpop log_unemp_rate filing_rate sd_share i.year if leftshare_begin >= `sd_med' & leftshare_begin!=. , fe cl(id)	
est store T1b_1		

// Table S2
xtreg log_cpas pctforeign logpop log_unemp_rate filing_rate sd_share log_neighbors  i.year if leftshare_begin!=. , fe cl(id)
est store S2a_1
xtreg log_cpas pctforeign logpop log_unemp_rate filing_rate sd_share log_neighbors  i.year  if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(id)
est store S2b_1

// Table S3		
xtreg log_cpas pctforeign logpop log_unemp_rate filing_rate sd_share log_rev_head i.year if leftshare_begin!=. , fe cl(id)
est store S3a_1
xtreg log_cpas pctforeign logpop log_unemp_rate filing_rate sd_share log_rev_head i.year if leftshare_begin >= `sd_med' & leftshare_begin!=. , fe cl(id)	
est store S3b_1				

// Table S8	
xtreg log_rev_head pctforeign logpop log_unemp_rate filing_rate sd_share i.year if leftshare_begin!=. , fe cl(id)
est store S8a_1
xtreg log_rev_head pctforeign logpop log_unemp_rate filing_rate sd_share i.year if leftshare_begin >= `sd_med' & leftshare_begin!=.  , fe cl(id)	
est store S8b_1


///////////////////////////////////////////
// Denmark	
///////////////////////////////////////////


use "./Datasets/Crossnational/Denmark.dta", clear
xtset tempid year

centile leftshare_begin if year==2001, c(50)
local sd_med = `r(c_1)'

// Table 1
xtreg log_net_social_head pctforeign logpop unemp_rate lowbracket_rate sd_share  i.year if leftshare_begin !=., fe cl(tempid)
est store T1a_2
xtreg log_net_social_head pctforeign logpop unemp_rate lowbracket_rate sd_share  i.year if leftshare_begin >= `sd_med' & leftshare_begin!=. , fe cl(tempid)
est store T1b_2	
		
// Table S2
xtreg log_net_social_head pctforeign logpop unemp_rate lowbracket_rate sd_share log_neighbor i.year, fe cl(tempid)
est store S2a_2
xtreg log_net_social_head pctforeign logpop unemp_rate lowbracket_rate sd_share log_neighbor i.year if leftshare_begin >= `sd_med' & leftshare_begin!=. , fe cl(tempid)
est store S2b_2

// Table S3
xtreg log_net_social_head pctforeign logpop unemp_rate lowbracket_rate sd_share log_rev  i.year, fe cl(tempid)
est store S3a_2
xtreg log_net_social_head pctforeign logpop unemp_rate lowbracket_rate sd_share log_rev i.year if leftshare_begin >= `sd_med' & leftshare_begin!=. , fe cl(tempid)
est store S3b_2

// Table S8
xtreg log_ownrev pctforeign logpop unemp_rate lowbracket_rate sd_share  i.year, fe cl(tempid)
est store S8a_2
xtreg log_ownrev pctforeign logpop unemp_rate lowbracket_rate sd_share  i.year  if leftshare_begin >= `sd_med' & leftshare_begin!=. , fe cl(tempid)
est store S8b_2


///////////////////////////////////////////
// Netherlands 
///////////////////////////////////////////


use "./Datasets/Crossnational/Netherlands.dta", clear
xtset tempid year	

centile leftshare_begin if year==2005, c(50)
local sd_med = `r(c_1)'

// Note: Omits pct_inactive. Including introduces listwise deletion due to data not being available for municipalities affected by mergers between 2009-2013.
// Note that adding pct_inactive does not alter conclusions

// Table 1
xtreg log_net_social_head pctforeign logpop lowbracket_rate  sd_share  i.year if leftshare_begin!=., fe cl(tempid)
est store T1a_3
xtreg log_net_social_head pctforeign logpop  lowbracket_rate  sd_share i.year if leftshare_begin >= `sd_med' &  leftshare_begin!=., fe cl(tempid)
est store T1b_3

// Table S2
xtreg log_net_social_head pctforeign logpop lowbracket_rate sd_share log_neighbor i.year if leftshare_begin!=., fe cl(tempid)
est store S2a_3
xtreg log_net_social_head pctforeign logpop lowbracket_rate sd_share log_neighbor i.year if leftshare_begin >= `sd_med' &  leftshare_begin!=., fe cl(tempid)
est store S2b_3	
		
// Table S3
xtreg log_net_social_head pctforeign logpop lowbracket_rate sd_share log_rev  i.year if leftshare_begin!=., fe cl(tempid)
est store S3a_3
xtreg log_net_social_head pctforeign logpop lowbracket_rate sd_share log_rev  i.year if leftshare_begin >= `sd_med' &  leftshare_begin!=., fe cl(tempid)			
est store S3b_3	

// Table S8			
xtreg log_ownrev pctforeign logpop lowbracket_rate sd_share  i.year if leftshare_begin!=., fe cl(tempid)	
est store S8a_3
xtreg log_ownrev pctforeign logpop lowbracket_rate sd_share  i.year  if leftshare_begin >= `sd_med' &  leftshare_begin!=., fe cl(tempid)
est store S8b_3

///////////////////////////////////////////
// Norway  
///////////////////////////////////////////


use "./Datasets/Crossnational/Norway.dta", clear

xtset tempid year

centile leftshare_begin if year==2004, c(50)
local sd_med = `r(c_1)'


// Table 1
xtreg  log_net_social_head pctforeign logpop log_unemp_rate log_lowbracket_rate  sd_share  i.year, fe cl(tempid)
est store T1a_4
xtreg  log_net_social_head pctforeign logpop log_unemp_rate log_lowbracket_rate  sd_share  i.year  if leftshare_begin > `sd_med' & leftshare_begin!=. , fe cl(tempid)
est store T1b_4

// Table S2
xtreg  log_net_social_head pctforeign logpop log_unemp_rate log_lowbracket_rate  sd_share log_neighbor i.year if leftshare_begin!=., fe cl(tempid)
est store S2a_4
xtreg  log_net_social_head pctforeign logpop log_unemp_rate log_lowbracket_rate  sd_share log_neighbor i.year  if leftshare_begin > `sd_med' & leftshare_begin!=. , fe cl(tempid)
est store S2b_4

// Table S3
xtreg  log_net_social_head pctforeign logpop log_unemp_rate log_lowbracket_rate  sd_share log_rev_head i.year if leftshare_begin!=., fe cl(tempid)
est store S3a_4
xtreg  log_net_social_head pctforeign logpop log_unemp_rate log_lowbracket_rate  sd_share  log_rev_head i.year  if leftshare_begin > `sd_med' & leftshare_begin!=. , fe cl(tempid)
est store S3b_4

// Table S8
xtreg  log_ownrev pctforeign logpop log_unemp_rate log_lowbracket_rate  sd_share i.year  if leftshare_begin!=. , fe cl(tempid)
est store S8a_4
xtreg  log_ownrev pctforeign logpop log_unemp_rate log_lowbracket_rate  sd_share i.year  if  leftshare_begin > `sd_med' & leftshare_begin!=. , fe cl(tempid)
est store S8b_4


///////////////////////////////////////////
// Sweden
///////////////////////////////////////////


use "./Datasets/Crossnational/Sweden.dta", clear

xtset tempid year

centile leftshare_begin if year==2002, c(50)
local sd_med = `r(c_1)'

// Table 1
xtreg log_net_social_head pctforeign logpop pct_inactive lowbracket_rate sd_share  i.year if leftshare_begin!=., fe cl(tempid)
est store T1a_5
xtreg log_net_social_head pctforeign logpop pct_inactive lowbracket_rate sd_share  i.year if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(tempid)
est store T1b_5

// Table S2
xtreg log_net_social_head pctforeign logpop pct_inactive lowbracket_rate sd_share log_neighbor i.year if leftshare_begin!=., fe cl(tempid)
est store S2a_5	
xtreg log_net_social_head pctforeign logpop pct_inactive lowbracket_rate sd_share log_neighbor i.year if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(tempid)
est store S2b_5	

// Table S3
xtreg log_net_social_head pctforeign logpop pct_inactive lowbracket_rate sd_share log_rev i.year if leftshare_begin!=., fe cl(tempid)		
est store S3a_5			
xtreg log_net_social_head  pctforeign logpop pct_inactive lowbracket_rate sd_share log_rev i.year if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(tempid)
est store S3b_5	

// Table S8
xtreg log_ownrev_head pctforeign logpop pct_inactive lowbracket_rate sd_share i.year if leftshare_begin!=., fe cl(tempid)		
est store S8a_5			
xtreg log_ownrev_head pctforeign logpop pct_inactive lowbracket_rate sd_share i.year if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(tempid)
est store S8b_5	


	
///////////////////////////////////////////
// France
///////////////////////////////////////////


use "./Datasets/Crossnational/France.dta", clear
xtset tempid year

centile total_left if year==2005, c(50)
local sd_med = `r(c_1)'

// Table 1
xtreg log_social_head pctforeign logpop log_unemp lowbracket_rate  i.year if total_left!=., fe cl(tempid)	
est store T1a_6			
xtreg log_social_head pctforeign logpop log_unemp lowbracket_rate  i.year if total_left > `sd_med' & total_left!=., fe cl(tempid)
est store T1b_6
	
// Table S2
xtreg log_social_head pctforeign logpop log_unemp lowbracket_rate log_neighbor i.year if total_left!=., fe cl(tempid)		
est store S2a_6			
xtreg log_social_head pctforeign logpop log_unemp lowbracket_rate log_neighbor if total_left >= `sd_med' & total_left!=., fe cl(tempid)
est store S2b_6	

// Table S3
xtreg log_social_head pctforeign logpop log_unemp lowbracket_rate log_rev_head i.year if total_left!=., fe cl(tempid)
est store S3a_6						
xtreg log_social_head pctforeign logpop log_unemp lowbracket_rate log_rev_head  i.year if total_left >= `sd_med' & total_left!=., fe cl(tempid)
est store S3b_6		

// Table S8
xtreg log_ownrev_head pctforeign logpop log_unemp lowbracket_rate   i.year if  total_left!=., fe cl(tempid)			
est store S8a_6		
xtreg log_ownrev_head pctforeign logpop log_unemp lowbracket_rate  i.year if total_left >= `sd_med' & total_left!=., fe cl(tempid)
est store S8b_6		

///////////////////////////////////////////
// Italy
///////////////////////////////////////////


use "./Datasets/Crossnational/Italy.dta", clear
xtset id year	

centile leftshare_begin if year==2003, c(50)
local sd_med = `r(c_1)'

// Note: 100% Lista Civica coded as . for leftshare_begin 

// Table 1
xtreg  log_social_head pctforeign logpop log_unemp_rate log_filing_rate  leftshare i.year, fe cl(id)
est store T1a_7
xtreg  log_social_head pctforeign logpop log_unemp_rate log_filing_rate  leftshare i.year  if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(id)
est store T1b_7

// Table S2
xtreg  log_social_head pctforeign logpop log_unemp_rate log_filing_rate  leftshare log_neighbor i.year, fe cl(id)
est store S2a_7
xtreg  log_social_head pctforeign logpop log_unemp_rate log_filing_rate  leftshare log_neighbor  i.year  if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(id)
est store S2b_7

// Table S3
xtreg  log_social_head pctforeign logpop log_unemp_rate log_filing_rate  leftshare log_rev i.year, fe cl(id)
est store S3a_7
xtreg  log_social_head pctforeign logpop log_unemp_rate log_filing_rate  leftshare log_rev i.year  if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(id)
est store S3b_7

// Table S8
xtreg  log_ownrev_head pctforeign logpop log_unemp_rate log_filing_rate  leftshare i.year, fe cl(id)
est store S8a_7
xtreg  log_ownrev_head pctforeign logpop log_unemp_rate log_filing_rate  leftshare i.year  if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(id)
est store S8b_7


///////////////////////////////////////////
// Spain 
///////////////////////////////////////////


use "./Datasets/Crossnational/Spain.dta", clear
xtset mid year	

centile leftshare_begin if year==2003, c(50)
local sd_med = `r(c_1)'

// Table 1
xtreg log_social_head pctforeign logpop pct_inactive  psoe income_head i.year, fe cl(mid)
est store T1a_8
xtreg log_social_head pctforeign logpop pct_inactive  psoe income_head i.year if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(mid)
est store T1b_8

// Table S2
xtreg log_social_head pctforeign logpop pct_inactive psoe income_head log_neighbor i.year, fe cl(mid)
est store S2a_8
xtreg log_social_head pctforeign logpop pct_inactive psoe income_head log_neighbor i.year if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(mid)
est store S2b_8

// Table S3
xtreg log_social_head pctforeign logpop pct_inactive psoe fin_totincome_head i.year, fe cl(mid)
est store S3a_8
xtreg log_social_head pctforeign logpop pct_inactive psoe fin_totincome_head i.year if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(mid)
est store S3b_8

// Table S8
xtreg log_ownrev_head pctforeign logpop pct_inactive  psoe  i.year, fe cl(mid)
est store S8a_8
xtreg log_ownrev_head pctforeign logpop pct_inactive  psoe  i.year if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(mid)
est store S8b_8

///////////////////////////////////////////
// Switz - National
///////////////////////////////////////////

use "./Datasets/Crossnational/Swiss_German.dta", clear
xtset bfsnr year	

centile leftshare_begin if year==2001, c(50)
local sd_med = `r(c_1)'
keep if pop > 250

// Table 1	
xtreg log_net_welfare_head pctforeign log_lowbracket logpop log_regunemployed sp i.year, fe cl(bfsnr)
est store T1a_9
xtreg log_net_welfare_head pctforeign log_lowbracket logpop log_regunemployed sp i.year if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(bfsnr)
est store T1b_9

// Table S2
xtreg log_net_welfare_head pctforeign log_lowbracket logpop log_regunemployed sp i.year log_neighbor, fe cl(bfsnr)
est store S2a_9
xtreg log_net_welfare_head pctforeign log_lowbracket logpop log_regunemployed sp i.year log_neighbor if leftshare_begin > = `sd_med' & leftshare_begin!=., fe cl(bfsnr)
est store S2b_9

// Table S3		
xtreg log_net_welfare_head pctforeign log_lowbracket logpop log_regunemployed log_rev_head sp i.year, fe cl(bfsnr)
est store S3a_9
xtreg log_net_welfare_head pctforeign log_lowbracket logpop log_regunemployed log_rev_head sp i.year  if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(bfsnr)
est store S3b_9

// Table S8			
xtreg  log_rev_head pctforeign log_lowbracket logpop log_regunemployed sp i.year, fe cl(bfsnr)
est store S8a_9
xtreg  log_rev_head pctforeign log_lowbracket logpop log_regunemployed sp i.year   if leftshare_begin >= `sd_med' & leftshare_begin!=., fe cl(bfsnr)
est store S8b_9		



///////////////////////////////////////////
// Appendix Table S1 
///////////////////////////////////////////


/////////////
// Belgium 
/////////////


use "./Datasets/Crossnational/Belgium.dta", clear
																
keep if year==2008
keep if pop > 500
centile pctforeign, c(95)
drop  if pctforeign > `r(c_1)'


reg pctsocial logpop pctforeign filing_rate rev_head, robust
est store S1a_1
reg log_cpas logpop pctforeign  filing_rate rev_head, robust
est store S1b_1	

/////////////
// Denmark
/////////////

use "./Datasets/Crossnational/Denmark.dta", clear

xtset tempid year
keep if year==2005

keep if pop>500
centile pctforeign, c(95)
drop  if pctforeign > `r(c_1)' 

reg pctsocial          logpop  pctforeign lowbracket_rate rev_head, robust
est store S1a_2
reg log_net_social_head logpop pctforeign lowbracket_rate rev_head, robust
est store S1b_2

/////////////
// Netherlands  
/////////////

use "./Datasets/Crossnational/Netherlands.dta", clear
keep if year==2008


keep if pop > 500
centile pctforeign, c(95)
drop  if pctforeign > `r(c_1)'	

reg pctsocial              logpop pctforeign lowbracket_rate rev_head, robust
est store S1a_3
reg log_net_social_head logpop pctforeign lowbracket_rate rev_head, robust
est store S1b_3

/////////////
// Norway 
/////////////

use "./Datasets/Crossnational/Norway.dta", clear
keep if year==2008

keep if pop > 500
centile pctforeign, c(95)
drop  if pctforeign > `r(c_1)'	

reg pctsocial logpop pctforeign lowbracket_rate  rev_head, robust
est store S1a_4
reg log_net_social_head logpop pctforeign lowbracket_rate  rev_head, robust
est store S1b_4
		
/////////////
// Sweden 
/////////////

use "./Datasets/Crossnational/Sweden.dta", clear
keep if year==2008

keep if pop > 500
centile pctforeign, c(95)
drop  if pctforeign > `r(c_1)'

reg pctsocial          logpop pctforeign lowbracket_rate rev_head, robust
est store S1a_5
reg log_net_social_head logpop pctforeign lowbracket_rate rev_head, robust
est store S1b_5
	
/////////////
// France
/////////////		
			
use "./Datasets/Crossnational/France.dta", clear
keep if year==2008

keep if pop>500
centile pctforeign, c(99)
drop  if pctforeign > `r(c_1)'	
				
reg pctsocial     logpop pctforeign lowbracket_rate rev_head, robust	
est store S1a_6
reg log_social_head logpop pctforeign lowbracket_rate rev_head, robust
est store S1b_6								
			
/////////////
// Italy
/////////////

use "./Datasets/Crossnational/Italy.dta", clear
keep if year==2008

keep if pop > 500	
centile pctforeign, c(95)
drop  if pctforeign > `r(c_1)'	

reg pctsocial     logpop pctforeign filing_rate rev_head , robust
est store S1a_7
reg log_social_head logpop pctforeign filing_rate rev_head, robust
est store S1b_7		

/////////////
// Spain
/////////////

use "./Datasets/Crossnational/Spain.dta", clear
keep if year==2008

keep if pop > 500
centile pctforeign, c(99)
drop  if pctforeign > `r(c_1)'	
	
reg pctsocial  logpop pctforeign income_head rev_head, robust
est store S1a_8
reg log_social_head logpop pctforeign  income_head rev_head , robust
est store S1b_8	


/////////////
// Switz
/////////////		
						
use "./Datasets/Crossnational/Swiss_German.dta", clear	
keep if year==2008

keep if pop > 500
centile pctforeign, c(95)
drop  if pctforeign > `r(c_1)'	

reg pctsocial logpop pctforeign lowbracket_rate rev_head, robust
est store S1a_9
reg log_net_welfare_head logpop pctforeign lowbracket_rate rev_head, robust
est store S1b_9	
		

///////////////////////////////////////////
// Output Tables
///////////////////////////////////////////

// Table 1
estout T1a_*, keep(pctforeign) cells(b(fmt(2)) se(fmt(2)par))  
estout T1b_*, keep(pctforeign) cells(b(fmt(2)) se(fmt(2)par)) 

// Table S1
estout S1a_*, keep(pctforeign) cells(b(fmt(2)) se(fmt(2)par))  
estout S1b_*, keep(pctforeign) cells(b(fmt(2)) se(fmt(2)par)) 
												
// Table S2
estout S2a_*, keep(pctforeign) cells(b(fmt(2)) se(fmt(2)par))  
estout S2b_*, keep(pctforeign) cells(b(fmt(2)) se(fmt(2)par)) 
								
// Table S3
estout S3a_*, keep(pctforeign) cells(b(fmt(2)) se(fmt(2)par))  
estout S3b_*, keep(pctforeign) cells(b(fmt(2)) se(fmt(2)par)) 

// Table S8
estout S8a_*, keep(pctforeign) cells(b(fmt(2)) se(fmt(2)par))  
estout S8b_*, keep(pctforeign) cells(b(fmt(2)) se(fmt(2)par)) 
		

